package com.lagou.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lagou.user.bean.Token;
import com.lagou.user.dao.TokenRepository;

@RestController
@RequestMapping("")
public class UserController {

    @Autowired
    TokenRepository tokenRepository;

    @GetMapping("/info/{token}")
    public ResponseEntity getInfo(@PathVariable("token") String token){

        Token t = tokenRepository.getByToken(token);
        if(t != null && StringUtils.isNotBlank(t.getEmail())){
            return ResponseEntity.ok(t.getEmail());
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

}
